package com.itheima.tanhua.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.tanhua.domain.UserInfo;
import com.itheima.tanhua.service.UserInfoService;
import com.itheima.tanhua.service.UserService;
import com.itheima.tanhua.utils.UserHolder;
import com.itheima.tanhua.vo.CountsVo;
import com.itheima.tanhua.vo.PageResult;
import com.itheima.tanhua.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author Lff
 * @date 2022/3/24
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    /**
     * 当前用户信息回显
     *
     * @param authorization
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity getUserInfo(@RequestHeader("Authorization") String authorization,
                                      Long userID) {
        //验证token合法性
       /* if (!JwtUtils.verifyToken(authorization)) {
            return ResponseEntity.status(401).body(null);
        }*/
        if (userID == null) {
            //如果没有传递过来userID就从token中拿去
//            userID = JwtUtils.getClaims(authorization).get("id", Long.class);
            //从ThreadLocal中获取id
            userID = UserHolder.getUser().getId();
        }
        UserInfoVo result = userInfoService.findById(userID);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改用户详细信息
     *
     * @param userInfo
     * @param authorization
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,
                                         @RequestHeader("Authorization") String authorization) {
        //检验token
       /* if (!JwtUtils.verifyToken(authorization)) {
            return ResponseEntity.status(401).body(null);
        }*/
        //获取userId并封装给UserInfo
//        Long id = (Long) JwtUtils.getClaims(authorization).get("id");
        //从ThreadLocal中获取id
        Long id = UserHolder.getUser().getId();
        //调用userInfoService更新
        userInfo.setId(id);
        userInfoService.updateUserInfo(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户头像更新
     * /users/header
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) {
        //获取用户id
        Long id = UserHolder.getUser().getId();
        userInfoService.updateHead(headPhoto, id);
        return ResponseEntity.ok(null);
    }

    /**
     * 发送验证码 用于修改手机号
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        userService.sendMsg(null);
        return ResponseEntity.ok(null);
    }

    /**
     * 校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        //获取验证码
        String verificationCode = (String) map.get("verificationCode");
        boolean verification = userService.checkVerificationCode(null, verificationCode);
        map.clear();
        map.put("verification", verification);
        return ResponseEntity.ok(map);
    }

    /**
     * 修改手机号
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map) {
        //获取新的手机号
        String phone = (String) map.get("phone");
        //调用service完成修改
        userService.upMobile(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 查看粉丝数量
     */
    @GetMapping("/counts")
    public ResponseEntity getFansCount() {
        CountsVo counts = userService.findCounts();
        return ResponseEntity.ok(counts);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * type：
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我
     * <p>
     * page	是    当前页数	1
     * <p>
     * pagesize	否	页尺寸    10
     * <p>
     * nickname	否	昵称
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity getUsersPage(@PathVariable("type") Integer type, //本次请求类型
                                       @RequestParam("page") Integer page,//当前页码
                                       @RequestParam("pagesize") Integer pagesize,//页尺寸
                                       @RequestParam(value = "nickname", required = false) String nickname) {
        PageResult pr = userInfoService.findUserPage(type, page, pagesize, nickname);
        return ResponseEntity.ok(pr);
    }

}
